using System.Configuration;
using System.Data.Linq.Mapping;
using System.IO;
using RomanJendrusz.Amazon.ETL.Infrastructure.Bus;
using RomanJendrusz.Amazon.ETL.Infrastructure.DataBase;
using System.Collections.Generic;
using RomanJendrusz.Amazon.ETL.Messages;
using RomanJendrusz.Amazon.ETL.Infrastructure.IoC;
using RomanJendrusz.Amazon.ETL.Infrastructure.IoC.Windsor;
using System.Reflection;
namespace AmazonTests.Utils
{
    public static class Helpers
    {
        public static IQuerySource<T> ToQuerySource<T>(this List<T> list) where T:class 
        {
            return new ListQuerySource<T>(list);
        }

        public static bool EqualTo(this AmazonRequest curRequest, AmazonRequest request)
        {
            request.CorrelationId = curRequest.CorrelationId;
            return request.Equals(curRequest);
        }

        public static IDependencyResolver InitializeIoC()
        {
            var windsorInitializer = new AppConfigWindsorInitializer();
            var resolver = new WindsorDependencyResolverAdapter(windsorInitializer);
            IoCGateway.Initialize(resolver);
            return resolver;
        }

        public static void ConfigureBus(IDependencyResolver resolver)
        {
            resolver.AddComponent<IBus, AmazonBus>();
            resolver.AddComponent<IRequestHandlerFactory, RequestHandlerFactory>();
            var ecsFactory = new AmazonECSFactory();
            ecsFactory.AddAwsUserKey("1FSSNYS4T9YMWMNCBQG2");
            resolver.AddInstance(ecsFactory);

        }

        public static void RebuildDatabase()
        {
            var file = @"Integration\Configuration\dbMapping.xml";
            var context = new DataBaseContext(new FileMappingSourceBuilder(file),
                                              new DataBaseConnectionFactory(ConfigurationManager.ConnectionStrings["fakeDB"].ConnectionString));
            context.DeleteDatabase();
            context.CreateDatabase();
        }
    }
}